/* eslint-disable */
// Created by wupeng @ 2019-11-11 11:40
// create vue component files
(function () {
  // 导入fs模块
  const fs = require('fs')
  // 导入path模块
  const path = require('path')

  // 获取组件目录文件
  let compsDir = path.join(path.resolve(__dirname, '../'), 'components')

  // 获取参数 作为组件名称
  let args = process.argv.splice(2)
  let compName = args[0]
  let compDir = path.join(compsDir, compName)
  fs.mkdir(compDir, {
    recursive: true
  }, (err) => {
    if (err) {
      throw err
    }

    console.log('组件' + compName + '目录创建成功！')
    // 创建js文件
    let jsFile = path.join(compDir, 'index.js')
    fs.writeFile(jsFile, [
      'export default {',
      "   name: '" + compName + "',",
      '   data() {',
      '       return {}',
      '   },',
      '   computed: {},',
      '   watch: {},',
      '   mounted: function() {',
      '       this.$nextTick(() => {})',
      '   },',
      '   methods: {}',
      '}',
      ''
    ].join('\n'), (err) => {
      if (err) {
        throw err
      }
      console.log('index.js创建成功！')
    })

    // 创建默认样式文件
    let defaultSassFile = path.join(compDir, 'default.sass')
    fs.writeFile(defaultSassFile, [
      '.' + compName
    ].join('\n'), (err) => {
      if (err) {
        throw err
      }
      console.log('default.sass创建成功！')
    })

    // 创建vue文件
    let vueFile = path.join(compDir, 'index.vue')
    fs.writeFile(vueFile, [
      '<template>',
      '   <div class=' + '"' + (compName + '-box') + '"' + '>',
      '       <div class="page-sub-header">',
      '           <span>' + compName + '</span>',
      '       </div>',
      '       <div class="app-page-body">',
      '           <div class="app-page-container"></div>',
      '       </div>',
      '   </div>',
      '</template>',
      '',
      '<script src="./index.js"></script>',
      '',
      '<style scoped lang="scss">',
      '   @import "./default.sass";',
      '</style>',
      ''
    ].join('\n'), (err) => {
      if (err) {
        throw err
      }
      console.log('index.vue创建成功！')
    })

    // 获取路由文件
    let routerDir = path.join(path.resolve(__dirname, '../'), 'router')
    fs.readFile(path.join(routerDir, 'index.js'), 'utf-8', (err, data) => {
      if (err) {
        throw err
      }
      let dataAry = data.split('Vue.use(VueRouter)')
      console.log(dataAry.length, dataAry)
      let upperCompName = compName.replace(compName[0], compName[0].toUpperCase())

      let top = dataAry[0]
      let newTop
      if (!!top) {
          newTop = [
            top,
            'import ' + upperCompName + " from '@/components/" + compName + "/index.vue'"
          ].join('')
      }

      let bottom = dataAry[1]
      let newBottom
      if (!!bottom) {
          let bottomAry = bottom.split('\n')
          let compAry = [
            '   // ' + compName,
            "   path: '/" + compName + "',",
            "   name: '" + upperCompName + "',",
            '   component: ' + upperCompName,
            '}, {'
          ]
          compAry.forEach((line, lineIndex) => {
            bottomAry.splice(4 + lineIndex, 0, line)
          })
          newBottom = bottomAry.join('\n')
      }

      fs.writeFile(path.join(routerDir, 'index.js'), [
        newTop,
        '',
        'Vue.use(VueRouter)',
        '',
        newBottom.trim(),
        ''
      ].join('\n'), (err) => {
        if (err) {
          throw err
        }
        console.log(path.join(routerDir, 'index.js') + '更新成功！')
      })
    })
  })
})()
